% Process Markers

%% Preparation

% Clear output window.
clc;

% Check for a valid subjectinfo object.
if ~exist('s', 'var')
    error('No subject information.');
end
if ~isa(s, 'SubjectInfo')
    error('Invalid subjectInfo.');
end


%% Load the file
load([s.FilepathBase 'markersExport.mat']);

%% Translate the markers

allowedMarkers = {'M90', 'M91', 'M92', 'M93', 'M99', 'M11', 'M22', 'M32', 'M33'};
for d = 1:length(s.Datasets)    
    m = markers.(['dataset' num2str(d)]);
    
    for i = 1:length(m)
        % check to see if it is an allowed marker, if not delete it.
        if isempty(strmatch(m(i,3), allowedMarkers, 'exact'))
            m(i,4) = {'Delete'};
        end
    end
    
    % Now exclude markers that are a result of the Subject V counting
    % configuration..
    for i = 2:length(m)-1
        if strcmp(m(i,4), 'None') % Only run if the marker hasn't been disabled yet.            
            curr = cell2mat(m(i,3));
            if curr(1)=='M' && length(curr) == 3 % Only run if the marker is of the Mxx style                
                prev = [curr(1) num2str(str2num(curr(2:3))-1)];
                next = [curr(1) num2str(str2num(curr(2:3))+1)];
                if strcmp(m(i-1,3), prev) && strcmp(m(i+1,3), next) ... % the prev and next are part of sequential numbers
                        && cell2mat(m(i+1,2)) - cell2mat(m(i-1,2)) < 1 % the prev. and next aren't more than 1 sec. apart
                    m(i,4) = {'Delete'};
                end
            end
        end
        
    end
    
    markers.(['dataset' num2str(d)]) = m;
end


%% Save the file
save ([s.FilepathBase 'markersImport.mat'], 'markers');